package com.biostime.qdingding.modules.qd.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.qdingding.common.config.Global;
import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.utils.StringUtils;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.qd.entity.Attach;
import com.biostime.qdingding.modules.qd.service.AttachService;

/**
 * 附件表Controller
 * @author king
 * @version 2016-01-11
 */
@Controller
@RequestMapping(value = "${adminPath}/qd/attach")
public class AttachController extends BaseController {

	@Autowired
	private AttachService attachService;
	
	@ModelAttribute
	public Attach get(@RequestParam(required=false) String id) {
		Attach entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = attachService.get(id);
		}
		if (entity == null){
			entity = new Attach();
		}
		return entity;
	}
	
	@RequiresPermissions("qd:attach:view")
	@RequestMapping(value = {"list", ""})
	public String list(Attach attach, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Attach> page = attachService.findPage(new Page<Attach>(request, response), attach); 
		model.addAttribute("page", page);
		return "modules/qd/attachList";
	}

	@RequiresPermissions("qd:attach:view")
	@RequestMapping(value = "form")
	public String form(Attach attach, Model model) {
		model.addAttribute("attach", attach);
		return "modules/qd/attachForm";
	}

	@RequiresPermissions(value={"qd:attach:edit,qd:attach:add"},logical = Logical.OR)
	@RequestMapping(value = "save")
	public String save(Attach attach, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, attach)){
			return form(attach, model);
		}
		attachService.save(attach);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/qd/attach/?repage";
	}
	
	@RequiresPermissions(value={"qd:attach:delete"})
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Attach attach, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				attachService.delete(new Attach(id));
			}
		}else{
			attachService.delete(attach);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/qd/attach/?repage";
	}

}